package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.SysMsgConstans;
import com.ruoyi.common.core.domain.CommonDto;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.SysMsg;
import com.ruoyi.system.domain.SysMsgUser;
import com.ruoyi.system.service.SysMsgService;
import com.ruoyi.system.mapper.SysMsgMapper;
import com.ruoyi.system.service.SysMsgUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author xdh
* @description 针对表【sys_msg】的数据库操作Service实现
* @createDate 2024-07-01 18:41:48
*/
@Service
public class SysMsgServiceImpl extends ServiceImpl<SysMsgMapper, SysMsg>
    implements SysMsgService{

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysMsgMapper sysMsgMapper;

    @Autowired
    private SysMsgUserService sysMsgUserService;

    @Override
    public IPage<SysMsg> pageMsg(CommonDto dto) {
        Long userId = tokenService.getLoginUser().getSysUser().getUserId();
        SysDept dept = tokenService.getLoginUser().getSysUser().getDept();
        if(dept == null){
            throw new ServiceException("当前用户未分配组织！");
        }
        return sysMsgMapper.selectByPage(new Page<>(dto.getPageNum(), dto.getPageSize()),dept.getDeptId(),userId);
//        return this.page(new Page<>(dto.getPageNum(), dto.getPageSize())
//                ,new LambdaQueryWrapper<SysMsg>().eq(SysMsg::getStatus,1).eq(SysMsg::getDeptId,dept.getDeptId()));
    }

    @Override
    public void orderCheck(Integer deptId,Integer userId,String content) {
        //消息列表新增
        SysMsg sysMsg = new SysMsg();
        sysMsg.setMsgType(1);
        sysMsg.setMsgTitle(SysMsgConstans.ORDER_CHECK_TITLE);
        sysMsg.setMsgContent(content);
        sysMsg.setStatus(1);
        sysMsg.setMsgPushTime(new Date());
        sysMsg.setMsgCreTime(new Date());
        sysMsg.setMsgFlag(0);
        sysMsg.setDeptId(deptId);
        this.save(sysMsg);
        //消息用户关联表新增
        SysMsgUser sysMsgUser = new SysMsgUser();
        sysMsgUser.setMsgId(sysMsg.getId());
        sysMsgUser.setUserId(userId);
        sysMsgUser.setStatus(1);
        sysMsgUser.setMsgFlag(0);
        sysMsgUser.setCreTime(new Date());
        sysMsgUserService.save(sysMsgUser);
    }

    @Override
    public void userCheck(Integer deptId,Integer userId,String content) {
        //消息列表新增
        SysMsg sysMsg = new SysMsg();
        sysMsg.setMsgType(2);
        sysMsg.setMsgTitle(SysMsgConstans.USER_CHECK_TITLE);
        sysMsg.setMsgContent(content);
        sysMsg.setStatus(1);
        sysMsg.setMsgPushTime(new Date());
        sysMsg.setMsgCreTime(new Date());
        sysMsg.setMsgFlag(0);
        sysMsg.setDeptId(deptId);
        this.save(sysMsg);
        //消息用户关联表新增
        SysMsgUser sysMsgUser = new SysMsgUser();
        sysMsgUser.setMsgId(sysMsg.getId());
        sysMsgUser.setUserId(userId);
        sysMsgUser.setStatus(1);
        sysMsgUser.setMsgFlag(0);
        sysMsgUser.setCreTime(new Date());
        sysMsgUserService.save(sysMsgUser);
    }

    @Override
    public void workCheck(Integer deptId,Integer userId,String content) {
        //消息列表新增
        SysMsg sysMsg = new SysMsg();
        sysMsg.setMsgType(3);
        sysMsg.setMsgTitle(SysMsgConstans.WORK_CHECK_TITLE);
        sysMsg.setMsgContent(content);
        sysMsg.setStatus(1);
        sysMsg.setMsgPushTime(new Date());
        sysMsg.setMsgCreTime(new Date());
        sysMsg.setMsgFlag(0);
        sysMsg.setDeptId(deptId);
        this.save(sysMsg);
        //消息用户关联表新增
        SysMsgUser sysMsgUser = new SysMsgUser();
        sysMsgUser.setMsgId(sysMsg.getId());
        sysMsgUser.setUserId(userId);
        sysMsgUser.setStatus(1);
        sysMsgUser.setMsgFlag(0);
        sysMsgUser.setCreTime(new Date());
        sysMsgUserService.save(sysMsgUser);

    }

    @Override
    public Integer get() {
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        Long deptId = sysUser.getDeptId();
        Long userId = sysUser.getUserId();
        return  sysMsgMapper.getWdCount(deptId,userId);
        //return this.count(new LambdaQueryWrapper<SysMsg>().eq(SysMsg::getMsgStatus,1).eq(SysMsg::getMsgFlag,0));
    }

    @Override
    public Boolean all() {
        Long userId = tokenService.getLoginUser().getSysUser().getUserId();
        return sysMsgUserService.updateMsgFlag1(userId);
    }

    @Override
    public Boolean info(Integer msgId) {
        Long userId = tokenService.getLoginUser().getSysUser().getUserId();
//        SysMsgUser sysMsg = new SysMsgUser();
//        sysMsg.s
//        sysMsg.setMsgFlag(1);
//        sysMsg.setUserId(userId.intValue());
        return sysMsgUserService.updateMsgFlag0ById(userId,msgId);
    }


    public Long getDeptId(){
        SysDept dept = tokenService.getLoginUser().getSysUser().getDept();
        if(dept == null ){
            throw new ServiceException("当前用户未分配组织！");
        }
        return dept.getDeptId();
    }

    public Long getUserId(){
        return  tokenService.getLoginUser().getSysUser().getUserId();
    }
}




